package com.xijia.serivce;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xijia.cache.annotation.XjCache;
import com.xijia.cache.annotation.XjCacheRefresh;
import com.xijia.entity.UserEntity;
import com.xijia.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;


@RestController
@Slf4j
public class MemberService extends ServiceImpl<UserMapper, UserEntity> {

    @Autowired
    private UserMapper userMapper;

    @RequestMapping("/getList")
    @XjCache
    public List<UserEntity> getList() {
        return userMapper.selectList(null);
    }


    @RequestMapping("/getUser")
    @XjCache
    public UserEntity getUser(String id, String name) {
        return userMapper.selectById(id);
    }


    @RequestMapping("/updUser")
    @XjCacheRefresh //刷新当前类缓存+当前id数据的缓存
    public Integer updUser(String id, String name) {
        UserEntity userEntity = new UserEntity();
        userEntity.setId(id);
        userEntity.setName(name);
        int result = userMapper.updateById(userEntity);
        return result;
    }

    @RequestMapping("/updUser2")
    @XjCacheRefresh //刷新当前类缓存+当前id数据的缓存
    public Integer updUser2(String id, String name) {
        int result = userMapper.update(null, new LambdaUpdateWrapper<UserEntity>()
                .set(UserEntity::getName, name)
                .set(UserEntity::getVersion, 0)
                .eq(UserEntity::getId, id)
        );
        return result;
    }


    @RequestMapping("/updUser3")
    @XjCacheRefresh //刷新当前类缓存+当前id数据的缓存
    public Integer updUser3(String id, String name) {
        int result = userMapper.upd(id, name);
        return result;
    }

    @RequestMapping("/updUser4")
    @XjCacheRefresh //刷新当前类缓存+当前id数据的缓存
    public boolean updUser4(String id, String name) {
        List<UserEntity > userEntitys = new ArrayList<>();

        UserEntity userEntity = new UserEntity();
        userEntity.setId("1");
        userEntity.setName(name);

        UserEntity userEntity2 = new UserEntity();
        userEntity2.setId("2");
        userEntity2.setName(name);

        userEntitys.add(userEntity);
        userEntitys.add(userEntity2);

        boolean b = this.updateBatchById(userEntitys,1024);
        return b;
    }



    @RequestMapping("/del")
    @XjCacheRefresh//刷新当前类缓存+当前id数据的缓存
    public Integer del(String id) {
        int result = userMapper.deleteById(id);
        return result;
    }

    @RequestMapping("/del2")
    @XjCacheRefresh //刷新当前类缓存+当前id数据的缓存
    public Integer del2(String id) {
        int result = userMapper.delete(new LambdaUpdateWrapper<UserEntity>().eq(UserEntity::getId,id));
        return result;
    }

    @RequestMapping("/del3")
    @XjCacheRefresh //刷新当前类缓存+当前id数据的缓存
    public Integer del3(String id) {
        int result = userMapper.del(id);
        return result;
    }

    @RequestMapping("/del4")
    @XjCacheRefresh //刷新当前类缓存+当前id数据的缓存
    public Integer del4(String id) {
        List<String> ids = new ArrayList<>();
        ids.add("1");
        ids.add("2");
        int result = userMapper.deleteBatchIds(ids);
        return result;
    }
}
